package com.bw.module_patrol.view

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.nfc.NdefMessage
import android.nfc.NdefRecord
import android.nfc.NfcAdapter
import android.nfc.NfcManager
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_patrol.R
import com.bw.module_patrol.databinding.ActivityNfcactivityBinding
import java.nio.charset.StandardCharsets
import java.util.*

/**
 * 工单-智慧园区-巡逻管理
 */
@Route(path = RouterPath.PATH_NFC_ACTIVITY)
class NFCActivity : BaseMvvmActivity<ActivityNfcactivityBinding>() {
    private var nfcAdapter: NfcAdapter? = null
    override fun observeDataChange() {
        val manager = getSystemService(Context.NFC_SERVICE) as NfcManager
        nfcAdapter = manager.defaultAdapter
    }

    override fun initView() {
        mDataBinding.settings.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_NFC_SETTING_ACTIVITY).navigation()
        }

        mDataBinding.view9.setOnClickListener {
            val intent = Intent(NfcAdapter.ACTION_NDEF_DISCOVERED)
            val record = NdefRecord.createTextRecord(":", "打卡成功")
            val message = NdefMessage(arrayOf(record))
            intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, arrayOf(message))
            onNewIntent(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        if (nfcAdapter != null) {
            val pendingIntent = PendingIntent.getActivity(
                this,
                0,
                Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                0
            )
            nfcAdapter!!.enableForegroundDispatch(this, pendingIntent, null, null)
        }
    }

    override fun onPause() {
        super.onPause()
        if (nfcAdapter != null) {
            nfcAdapter!!.disableForegroundDispatch(this)
        }
    }

    @SuppressLint("MissingSuperCall")
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent!!.action) {
            val rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
            if (rawMessages != null) {
                val messages = arrayOfNulls<NdefMessage>(rawMessages.size)
                for (i in rawMessages.indices) {
                    messages[i] = rawMessages[i] as NdefMessage
                }
                // 处理接收到的 NFC 数据
                processNdefMessages(messages)
            }
        }
    }
    private fun processNdefMessages(messages: Array<NdefMessage?>) {
        for (message in messages) {
            if (message != null) {
                for (record in message.records) {
                    if (record.tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(
                            record.type,
                            NdefRecord.RTD_TEXT
                        )
                    ) {
                        // 解码文本数据
                        val text = String(record.payload, StandardCharsets.UTF_8)
                        // 显示或处理文本数据
                        Log.d("NFC", "NFC模拟: $text")
                    }
                }
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_nfcactivity


}